package cn.mumzone.pos.presenter;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.pos.event.ChangeShiftEvent;
import cn.mumzone.pos.model.ScheduleDetailEntity;
import cn.mumzone.pos.model.ScheduleEmployeeEntity;
import cn.mumzone.pos.model.ScheduleResultEntity;
import cn.mumzone.pos.model.ShopUsableInfoEntity;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.model.source.ScheduleRepository;
import cn.mumzone.pos.presenter.contract.ScheduleContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by qiuzhenhuan on 2019/7/10.
 */


public class SchedulePresenter extends AbsPresenter<ScheduleContract.View, ScheduleRepository> implements ScheduleContract.Presenter {

    ReserveRepository reserveRepository;

    @Override
    protected void initOtherRepository() {
        reserveRepository = new ReserveRepository();
    }

    @Override
    public void getAllScheduleInfo() {
        ApiCallBack<List<String>> callBack = new ApiCallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> scheduleEntities) {
                mView.showScheduleStatus(scheduleEntities);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
            }
        };
        mRepo.getScheduleStatus(callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void getScheduleDetail(String scheduleDay) {
        ApiCallBack<ScheduleResultEntity> callBack = new ApiCallBack<ScheduleResultEntity>() {
            @Override
            public void onSuccess(ScheduleResultEntity scheduleDetailEntity) {
                Collections.sort(scheduleDetailEntity.getScheduleList(), new Comparator<ScheduleDetailEntity>() {
                    @Override
                    public int compare(ScheduleDetailEntity o1, ScheduleDetailEntity o2) {
                        return Integer.parseInt(o1.getScheduleStartTime()) - Integer.parseInt(o2.getScheduleStartTime());
                    }
                });
                mView.showScheduleDetail(scheduleDetailEntity.getScheduleList());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.getScheduleInfo(callBack, scheduleDay);
        addSubscrebe(callBack);
    }

    @Override
    public void doChangeShift(ScheduleEmployeeEntity fromEmployee, ScheduleEmployeeEntity toEmployee) {
        mView.showLoadingDialog();
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                mView.dismissLoadingDialog();
                mView.showChangeShiftSuccess(result);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        mRepo.changeShift(callBack, fromEmployee, toEmployee);
        addSubscrebe(callBack);
    }

    @Override
    public void addPresonShift(ScheduleDetailEntity entity, String scheduleDay, String employeeId) {
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                mView.showAddPersonShiftSuccess(result);
                getAllScheduleInfo();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.addEmployeeToSchedule(entity.getScheduleId(), entity.getScheduleName(), scheduleDay, employeeId, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void removePersonFromShift(String scheduleDay, String scheduleId, String employeeId, String startTime, String endTime) {
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                mView.showRemovePersonShiftSuccess(result);
                getAllScheduleInfo();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.removeEmployeeFromSchedule(callBack, scheduleDay, scheduleId, employeeId,
                AppDateUtil.getDateToString(Long.valueOf(startTime), "HH:mm"),
                AppDateUtil.getDateToString(Long.valueOf(endTime), "HH:mm"));
        addSubscrebe(callBack);
    }

    @Override
    public void getServerList() {
        ApiCallBack<ShopUsableInfoEntity> callBack = new ApiCallBack<ShopUsableInfoEntity>() {
            @Override
            public void onSuccess(ShopUsableInfoEntity reserveInfoEntity) {
                mView.showServerList(reserveInfoEntity.getServers());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        reserveRepository.getShopUsableServerList("", callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void checkEmployeeDoChangeShift(String scheduleDay, String scheduleId, String employeeId, String startTime, String endTime) {
        mView.showLoadingDialog();
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                mView.dismissLoadingDialog();
                mView.showCheckDoChangeShiftSuccess();
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        mRepo.checkEmployeeDoChangeShift(callBack, scheduleDay, scheduleId, employeeId,
                AppDateUtil.getDateToString(Long.valueOf(startTime), "HH:mm"),
                AppDateUtil.getDateToString(Long.valueOf(endTime), "HH:mm"));
        addSubscrebe(callBack);
    }

    @Override
    public boolean isNowSchedule(String selectedDay, ScheduleDetailEntity selectedSchedule) {
        String startTime = String.format("%s %s", selectedDay, AppDateUtil.getDateToString(Long.valueOf(selectedSchedule.getScheduleStartTime()), "HH:mm"));
        String endTime = String.format("%s %s", selectedDay, AppDateUtil.getDateToString(Long.valueOf(selectedSchedule.getScheduleEndTime()), "HH:mm"));

        long nowTime = System.currentTimeMillis();
        long startTimeMillis = AppDateUtil.getStringToDate(startTime, "yyyy-MM-dd HH:mm");
        long endTimeMillis = AppDateUtil.getStringToDate(endTime, "yyyy-MM-dd HH:mm");

        if ((nowTime >= startTimeMillis && nowTime < endTimeMillis) || nowTime > endTimeMillis) {
            mView.showError("班次已生效，不能进行操作");
            return true;
        }
        return false;
    }

    public void initEvent() {
        Subscription subscription = RxBus.getDefault().toObservable(ChangeShiftEvent.class).subscribe(new Action1<ChangeShiftEvent>() {
            @Override
            public void call(ChangeShiftEvent changeShiftEvent) {
                getScheduleDetail(changeShiftEvent.getScheduleDay());
            }
        });
        addSubscrebe(subscription);
    }
}
